package com.chinasoft.userservice.controller;

import com.chinasoft.ms.dto.UserDTO;
import com.chinasoft.ms.pojo.PageItem;
import com.chinasoft.ms.pojo.Result;
import com.chinasoft.ms.pojo.User;
import com.chinasoft.userservice.service.UserService;
import com.chinasoft.userservice.utils.JwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class UserController {

    @Autowired
    private UserService userService;


    @PostMapping("/api/user/login")
    public Result<String> login(
            // 将 json字符串转换为 pojo对象
            @RequestBody User user)  {
        System.out.println("--------------------login--------------------------");
        System.out.println(user);
        // 判断用户名称和密码是否正确
        User dbUser = userService.checkUser(user);
        System.out.println(dbUser);

        if (dbUser != null) {
            // 得到令牌
            String token = JwtUtils.createToken(dbUser.getId().toString(), dbUser.getUsername());
            return new Result<String>(200, "登录成功！", token);
        } else {
            return new Result<String>(500, "登录失败");
        }

    }

//    @GetMapping("/api/user/info")
    public Result<User> getUserInfo(@RequestHeader("Authorization") String token, @RequestHeader(value="userId", required = false) String strUserId) {
        System.out.println("--------------------info----------------------");
        System.out.println("header userId is" + strUserId);
        System.out.println(token);
        if (JwtUtils.verifyToken(token)) {
            System.out.println("令牌是 ok");
            String strId = JwtUtils.getAudience(token);
            System.out.println(strId);
            Long userId = Long.parseLong(strId);
            // 根据id查询用户的信息
            User user = userService.getUserWithId(userId);
            System.out.println(user);
            return new Result<User>(200, "用户信息获取成功！", user);
        }

        return new Result<User>(400, "无效令牌！");
    }

    @GetMapping("/api/user/info")
    public Result getUserInfo2(@RequestHeader(value="userId", required = false) String strUserId) {
        System.out.println("------------------info-------------");
        // 获取用户的 ID
        long userId = Long.parseLong(strUserId);
        User user = userService.getUserWithId(userId);
        Result result = new Result(200, "user info 成功 ！", user);
        return result;

    }


    @GetMapping("/api/user/page")
    public Result<PageItem<UserDTO>> getUserPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Long id,
            @RequestParam(required = false) String username,
            @RequestParam(required = false) String phone,
            @RequestParam(required = false) String address) {

        User query = new User();
        query.setId(id);
        query.setUsername(username);
        query.setPhone(phone);
        query.setAddress(address);

        PageItem<UserDTO> page = userService.queryByPageCond(pageNum, pageSize, query);
        return new Result<>(200, "查询成功", page);
    }

    @PostMapping("/api/user")
    public Result addUser(@RequestBody UserDTO userDTO) {
        boolean success = userService.addUser(userDTO);
        return success ? new Result<>(200, "添加成功") : new Result<>(500, "添加失败");
    }

    @PutMapping("/api/user")
    public Result updateUser(@RequestBody UserDTO userDTO) {
        boolean success = userService.updateUser(userDTO);
        return success ? new Result<>(200, "更新成功") : new Result<>(500, "更新失败");
    }

    @DeleteMapping("/api/user/{id}")
    public Result deleteUser(@PathVariable Long id) {
        boolean success = userService.deleteUser(id);
        return success ? new Result<>(200, "删除成功") : new Result<>(500, "删除失败");
    }

    @PostMapping("/api/user/deleteBatch")
    public Result deleteBatch(@RequestBody List<Long> ids) {
        boolean success = userService.deleteBatch(ids);
        return success ? new Result<>(200, "批量删除成功") : new Result<>(500, "批量删除失败");
    }
    @PostMapping("/api/user/register")
    public Result register(@RequestBody UserDTO userDTO) {
        System.out.println("注册请求接收，数据：" + userDTO); // 添加日志
        boolean success = userService.register(userDTO);
        return success ? new Result<>(200, "注册成功") : new Result<>(500, "注册失败");
    }



}
